﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xant.FlowEngine.Interface;

namespace Xant.FlowEngine.Core
{
    public abstract class AbstractActivity : AbstractFlowObject
    {
        #region 变量声明
        private static string suffixActivity = "Activity";

        protected Status currentStatus = Status.None;

        public delegate void StatusChangingHandler(AbstractActivity activity, Status newStatus, ref bool cancel);
        public StatusChangingHandler StatusChanging;
        public delegate void StatusChangedHandler(AbstractActivity activity);
        public StatusChangedHandler StatusChanged;

        #endregion

        #region 属性

        public string Type
        {
            get
            {
                var type = this.GetType().Name;
                //如果类名以Activity后缀结尾，则省略后缀，以获得更好的可读性
                if (type.EndsWith(suffixActivity))
                {
                    type = type.Substring(0, type.Length - suffixActivity.Length);
                }
                return type;
            }
        }

        public AbstractProcess OwnerProcess { get; set; }

        public Guid ProcessId 
        {
            get
            {
                return OwnerProcess==null ? Guid.Empty : OwnerProcess.Id;
            }
        }
        
        /// <summary>
        /// 活动编号，流程内唯一
        /// <remarks>
        /// 将通过活动编号来查找活动对象</remarks>
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 活动名称
        /// </summary>
        public string Name { get; set;  }

        /// <summary>
        /// 活动状态
        /// </summary>
        public Status Status
        {
            get { return currentStatus; }
            internal set
            {
                if (StatusChanging != null)
                {
                    bool cancel = false;
                    StatusChanging(this, value, ref cancel);
                    if (cancel)
                    {
                        return;
                    }
                }
                currentStatus = value;
                if (StatusChanged != null)
                {
                    StatusChanged(this);
                }
            }
        }

        public DateTime? EntryTime { get; internal set; }

        public DateTime? LeaveTime { get; internal set; }

        #endregion

        #region 构造函数
        public AbstractActivity()
        {
        }

        public AbstractActivity(string code): this()
        {
            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentNullException();
            }
            this.Code = code;
        }

        public AbstractActivity(string code, string name): this(code)
        {
            this.Name = name;
        }
        #endregion

        /// <summary>
        /// 激动活动节点
        /// <remarks>继承类通过覆写此方法来处理具体的活动处理业务逻辑，例如发邮件、生成工作项等</remarks>
        /// </summary>
        public virtual void Entry()
        {
            Console.WriteLine(string.Format("活动开始执行，活动类型：{0}，活动名称：{1}", this.GetType().Name, this.Name));
            this.Status = Status.Running;
        }

        protected virtual void Leave()
        {
            Console.WriteLine(string.Format("活动执行结束，活动类型：{0}，活动名称：{1}", this.GetType().Name, this.Name));
            this.Status = Status.Completed;
        }

        protected virtual void Cancel()
        {
            this.Status = Status.Canceled;
        }

        public void Rollback()
        {

        }

        public AbstractActivity Copy()
        {
            var copy = Clone();
            copy.Code = this.Code;
            copy.Name = this.Name;
            return copy;
        }

        protected abstract AbstractActivity Clone();

    }
}
